package si.yun.web;

import com.aliyuncs.exceptions.ClientException;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import si.yun.entity.SyUser;
import si.yun.service.SyAccountSelService;
import si.yun.utils.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

/**
 * @ClassName: 查询账户WEB层
 * @Description: TODO
 * @Author: 柴秋生
 * @Date 2021/8/17 16:04
 */
@Controller
@RequestMapping("/account")
@PropertySource("classpath:config.properties")
public class SyAccountSelController {
    @Autowired
    private SyAccountSelService syAccountSelService;

    @Autowired
    private TestAliUpload testAliUpload;
    /*后台用户获取路径*/
    @Value("${path1}")
    private String path;

    /**
     * @Description  修改管理人员的图片
     * @return si.yun.utils.JsonData
     * @Date 2021/9/4 17:30
     **/
    @PostMapping("/updAvatarUrl")
    @ResponseBody
    public JsonData updAvatarUrl(@RequestParam("file") MultipartFile files, HttpSession session, Integer id) throws Exception {
        String newName = uploadFile(files);
        //插入数据
        int up=syAccountSelService.updAvatarUrl(id,newName);
        return up != 0 ? JsonData.buildSuccess(up) : JsonData.buildError("服务器异常！请耐心等待修复！");
    }
    public String  uploadFile(@RequestParam("file") MultipartFile files) throws IllegalStateException, IOException {
        //上传到服务器
        StorePath upload = testAliUpload.upload(files);
        String newName = upload.getFullPath();
        return newName;
    }
    /**
     * @Author : 李振海
     * @Date : 2021/9/14 10:35
     * @Param : [id, newpwd]
     * @Return : int
     * 修改密码
     */
    @RequestMapping("/updUserPwd")
    @ResponseBody
    public int updUserPwd(Integer id, String newpwd) {
        String s = Md5Utils.MD5andSAL(newpwd);
        int  updUserPwd=syAccountSelService.updUserPwd(id,s);
        return updUserPwd;
    }
	/**
     * @Description 获取密码
     * @return java.lang.String
     * @Date 2021/12/24 10:47
     */
    @RequestMapping("/getPassword")
    @ResponseBody
    public Boolean getPassword(Integer id,String conpwd,String account){
        String password = syAccountSelService.getPassword(id,account);
        boolean verify = Md5Utils.verify(conpwd, password);
        return verify;
    }
    /**
     * @Description 获取账号
     * @return java.lang.Boolean
     * @Date 2021/12/27 20:53
     */
    @RequestMapping("/getAccount")
    @ResponseBody
    public Boolean getAccount(String account){
        String account1 = syAccountSelService.getAccount(account);
        boolean account2 = account.equals(account1);
        return account2;
    }
    /**
     * @Description 获取用户手机号
     * @return java.lang.String
     * @Date 2021/12/28 14:40
     */
    @RequestMapping("/getPhoneNumber")
    @ResponseBody
    public JsonData getPhoneNumber(String account){
        String account1 = syAccountSelService.getPhoneNumber(account);
        return account1!=null?JsonData.buildSuccess(account1):JsonData.buildError("服务器错误");
    }
    /**
     * @Description 短信验证码
     * @return si.yun.utils.JsonData
     * @Date 2021/12/29 10:16
     */
    @RequestMapping("/getValidation")
    @ResponseBody
    public JsonData getValidation(String phone) throws ClientException {
        String code = GetRandomUtil.randomCode();
        SendMessage.sendSms(phone,code);
        return code!=null?JsonData.buildSuccess(code):JsonData.buildError("服务器错误");
    }
    /**
     * @Description 修改密码
     * @return int
     * @Date 2021/12/29 10:19
     */
    @RequestMapping("/setPwd")
    @ResponseBody
    public int updUserPwd(String newpwd,String account) {
        String npwd = Md5Utils.MD5andSAL(newpwd);
        int  updUserPwd=syAccountSelService.setPwd(npwd,account);
        return updUserPwd;
    }

}
